﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Common.ErrorLogging;
using System.Xml.Serialization;
using System.Xml;

namespace Sinacor.Infra.UI.ErrorLoggingEnvironment
{
    public class UserInterfaceEnvironmentData : LoginEnvironmentData
    {
        [XmlArray("ScreensResolutions")]
        [XmlArrayItem("Resolution")]
        public List<string> ScreensResolutions { get; set; }

        [XmlAttribute("SinacorMenuPath")]
        public string SinacorMenuPath { get; set; }

        [XmlAttribute("SinacorWindowTitle")]
        public string SinacorWindowTitle { get; set; }

        protected override string ToStringOverride()
        {
            StringBuilder logData = new StringBuilder(base.ToStringOverride());

            if (!string.IsNullOrEmpty(this.SinacorUserName) ||
                !string.IsNullOrEmpty(this.SinacorCompany) ||
                !string.IsNullOrEmpty(this.SinacorModule))
            {
                logData.AppendLine();
            }

            if (this.ScreensResolutions != null && this.ScreensResolutions.Count > 0)
            {
                logData.AppendFormat("ScreensResolutions:\t{0}", string.Join(", ", this.ScreensResolutions.ToArray()));
                logData.AppendLine();
            }

            if (!string.IsNullOrEmpty(this.SinacorMenuPath))
            {
                logData.AppendFormat("SinacorMenuPath:\t\t{0}", this.SinacorMenuPath);
                logData.AppendLine();
            }

            if (!string.IsNullOrEmpty(this.SinacorWindowTitle))
            {
                logData.AppendFormat("SinacorWindowTitle:\t{0}", this.SinacorWindowTitle);
                logData.AppendLine();
            }

            return logData.ToString();
        }

        public override void WriteTo(XmlWriter writer)
        {
            base.WriteTo(writer);

            if (this.ScreensResolutions != null && this.ScreensResolutions.Count > 0)
            {
                writer.WriteElementString("ScreensResolutions", string.Join(", ", this.ScreensResolutions.ToArray()));
            }

            if (!string.IsNullOrEmpty(this.SinacorMenuPath))
            {
                writer.WriteElementString("SinacorMenuPath", this.SinacorMenuPath);
            }

            if (!string.IsNullOrEmpty(this.SinacorWindowTitle))
            {
                writer.WriteElementString("SinacorWindowTitle", this.SinacorWindowTitle);
            }
        }
    }
}
